import java.sql.*;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        //查询
        //m1();
        //条件查询
        //m2();
        //增加
        //m3();
        //删除
        //m4();
        //修改
        //m5();

    }


    private static void m5() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            //2.获取连接
            String url="jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            try {
                Connection con = DriverManager.getConnection(url, "root", "123456");
                if(con!=null){
                    System.out.println("连接成功");
                }

                Scanner sc = new Scanner(System.in);
                System.out.println("输入id：");
                int idd = sc.nextInt();
                System.out.println("输入修改的名字：");
                String nam = sc.next();
                System.out.println("输入修改的密码：");
                String pa = sc.next();

                //3.写sql
                String sql="update user2 set name=?,pass=? where id=?";

                //4.预编译sql
                PreparedStatement preparedStatement = con.prepareStatement(sql);

                //5.有参数设参数，复制一行ctrl+d
                preparedStatement.setString(1, nam);
                preparedStatement.setString(2, pa);
                preparedStatement.setInt(3,idd);

                //6.执行sql
                int i = preparedStatement.executeUpdate();
                System.out.println("3");
                //7.建立结果集
                if(i>0){
                    System.out.println("更新成功");
                }else{
                    System.out.println("未找到此id");
                }
                //8.关闭资源

                preparedStatement.close();
                con.close();

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }




    private static void m4() {  try {
        Class.forName("com.mysql.cj.jdbc.Driver");

        //2.获取连接
        String url="jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
        try {
            Connection con = DriverManager.getConnection(url, "root", "123456");
            if(con!=null){
                System.out.println("连接成功");
            }

            Scanner sc = new Scanner(System.in);
            System.out.println("输入删除的id：");
            int idd = sc.nextInt();

            //3.写sql
            String sql="delete from user2 where id=?";

            //4.预编译sql
            PreparedStatement preparedStatement = con.prepareStatement(sql);

            //5.有参数设参数
            preparedStatement.setInt(1, idd);

            //6.执行sql
            int i = preparedStatement.executeUpdate();

            //7.建立结果集
            if(i>0){
                System.out.println("删除成功");
            }else{
                System.out.println("未找到此id");
            }
            //8.关闭资源

            preparedStatement.close();
            con.close();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }


    }

    private static  void m3() {  try {

        Class.forName("com.mysql.cj.jdbc.Driver");

        //2.获取连接
        String url="jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
        try {
            Connection con = DriverManager.getConnection(url, "root", "123456");
            if(con!=null){
                System.out.println("连接成功");
            }

            Scanner sc = new Scanner(System.in);
            System.out.println("输入的姓名：");
            String nam = sc.next();
            System.out.println("输入的密码：");
            String pa = sc.next();

            //3.写sql
            String sql="insert into user2 (name,pass)values (?,?)";

            //4.预编译sql
            PreparedStatement preparedStatement = con.prepareStatement(sql);

            //5.有参数设参数
            preparedStatement.setString(1,nam);
            preparedStatement.setString(2,pa);

            //6.执行sql
            int i = preparedStatement.executeUpdate();

            //7.建立结果集
           if(i>0){
               System.out.println("添加成功");
           }else{
               System.out.println("添加失败");
           }

            //8.关闭资源

            preparedStatement.close();
            con.close();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }


    }

    private static void m2() {
        try {

            Class.forName("com.mysql.cj.jdbc.Driver");

            //2.获取连接
            String url="jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            try {
                Connection con = DriverManager.getConnection(url, "root", "123456");
                if(con!=null){
                    System.out.println("连接成功");
                }

                Scanner sc = new Scanner(System.in);
                System.out.println("输入查询的姓名：");
                String nam = sc.next();

                //3.写sql
                String sql="select * from user1 where name like ?";

                //4.预编译sql
                PreparedStatement preparedStatement = con.prepareStatement(sql);

                //5.有参数设参数
                preparedStatement.setString(1,"%"+nam+"%");
                //6.执行sql
                ResultSet resultSet = preparedStatement.executeQuery();

                //7.建立结果集
                while(resultSet.next()){
                    int id = resultSet.getInt(1);
                    String name = resultSet.getString(2);
                    String pass = resultSet.getString(3);
                    System.out.println(id+"\t"+name+"\t"+pass);
                }

                //8.关闭资源
                resultSet.close();
                preparedStatement.close();
                con.close();

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }


    }

    private static void m1() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            //2.获取连接
            String url="jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            try {
                Connection con = DriverManager.getConnection(url, "root", "123456");
                if(con!=null){
                    System.out.println("连接成功");
                }

                //3.写sql
                String sql="select * from user2";

                //4.预编译sql
                PreparedStatement preparedStatement = con.prepareStatement(sql);

                //5.有参数设参数

                //6.执行sql
                ResultSet resultSet = preparedStatement.executeQuery();

                //7.建立结果集
                while(resultSet.next()){
                    int id = resultSet.getInt(1);
                    String name = resultSet.getString(2);
                    String pass = resultSet.getString(3);
                    System.out.println(id+"\t"+name+"\t"+pass);
                }

                //8.关闭资源
                resultSet.close();
                preparedStatement.close();
                con.close();

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}